<?php
    $img = $_FILES["img"];
    if ($img["error"] > 0)
    {
        echo "file upload failed! ".$img["error"]."<br>";
    }
    else
    {
        echo $img["name"]."<br>";
        echo $img["type"]."<br>";
        echo ($img["size"]/1024)."KB<br>";
        echo $img["tmp_name"]."<br>";
        echo $img["error"]."<br>";
        echo $_POST["img_name"]."<br>";

        $img_type = array("image/jpeg", "image/jpg", "image/png");

        foreach ($img_type as $type)
        {
            if ($img["type"] == $type)
            {
                if (($img["size"]/1024) < 10240)
                {
                    //open tempfile
                    $file = fopen($img["tmp_name"], "r");
                    if ($file)
                    {
                        //open newfile
                        $res = fopen("./img/".$img["name"], "a");
                        if ($res)
                        {
                            $img_name = htmlspecialchars($_POST["img_name"]);
                            $conn = mysqli_connect("localhost", "root", "***", "test");
                            if ($conn)
                            {
                                $addr = $img["name"];
                                $sql = "insert into img(img_name, img_addr) values('$img_name','$addr');";
                                if(mysqli_query($conn, $sql))
                                {
                                    while (!feof($file))
                                    {
                                        $line = fgets($file);
                                        fwrite($res, $line);
                                    }
                                    fflush($res);
                                    fclose($res);
                                    fclose($file);
                                    echo "<script>alert('file upload success!');window.location.href='index.html';</script>";
                                }
                                else
                                {
                                    fclose($file);
                                    fclose($res);
                                    unlink("./img/".$img["name"]);
                                    echo "file upload failed! mysql query failed.";
                                }
                                mysqli_close($conn);
                            }
                            else
                            {
                                fclose($file);
                                fclose($res);
                                unlink("./img/".$img["name"]);
                                echo "file upload failed! mysql connect failed.";
                            }
                        }
                        else
                        {
                            fclose($file);
                            echo "file upload failed! open newfile failed.";
                        }
                        
                    }
                    else
                    {
                        echo "file upload failed! open tmpfile failed.";
                    }
                }
                break;
            }
        }
    }
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Upload</title>
</head>
<body>
    <a href="index.html">Back</a>
</body>
</html>